<?php
/**
 *  This file is part of CI-ActiveRecord
 *
 *  CI-ActiveRecord is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CI-ActiveRecord is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CI-ActiveRecord.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * ActiveRecord_Event
 */
class ActiveRecord_Event {
        
    /*
     * Events
     */
    const PRE_SAVE  = 'preSave';
    const POST_SAVE = 'postSave';
    
    /**
     * @var mixed $_invoker the handler which invoked this event
     * @var array $_options an array of options
     */
    protected $_invoker;
    protected $_options = array();
    
    /**
     * __construct
     */
    public function __construct(ActiveRecord $invoker, $name)
    {
        log_message('debug', "Event $name triggered");
        $this->_invoker = $invoker;
        $this->_name = $name;
        $this->getInvoker()->triggerEvent($this);
    }
    
    /**
     * __get
     * returns the value of an option
     *
     * @param string $option    the name of the option
     * @return mixed
     */
    public function __get($option)
    {
        if ( ! isset($this->_options[$option])) {
            return null;
        }
        
        return $this->_options[$option];
    }
    
    /**
     * __set
     * sets the value of an option
     *
     * @param string $option        the name of the option
     * @param mixed $value          the value of the given option
     * @return ActiveRecord_Event   this object
     */
    public function __set($option, $value)
    {
        $this->_options[$option] = $value;

        return $this;
    }

    /**
     * getInvoker
     * returns the handler that invoked this event
     *
     * @return ActiveRecord
     */
    public function getInvoker()
    {
        return $this->_invoker;
    }
    
    public function getName()
    {
        return $this->_name;
    }

    /**
     * skipOperation
     * skips the next operation
     * an alias for __set('skipOperation', true)
     *
     * @return Doctrine_Event   this object
     */
    public function skipOperation()
    {
        $this->_options['skipOperation'] = true;
    
        return $this;
    }
}